package com.turkline.CatchMePolygon;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import aurelienribon.tweenengine.equations.Quad;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.GeneralButton;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.impl.GLGame;
import com.badlogic.androidgames.framework.impl.GLScreen;
import com.badlogic.androidgames.framework.math.OverlapTester;
import com.badlogic.androidgames.framework.math.Vector2;

public class StartScreen extends GLScreen
{
	float stateTime;
	GeneralButton playButton;
	boolean animating = false;
	
	float logoLeftX = -256;
	float logoRightX = 976;
	
	Help help;
	private TouchEvent e;
	private int dotCount = 0;
	private int dotCounter = 0;
	
	public StartScreen(Game game, int maxSprites, int camWidth, int camHeight)
	{
		super(game, maxSprites, camWidth, camHeight);
		playButton = new GeneralButton();
		stateTime = 0;
		
	}

	@Override
	public void update(float deltaTime)
	{
		if(this.glGame.firtTimeAdReceived)
		{
			if(!animating)
			{
				if(Settings.showHelpStartScreen)
				{
					help.update(deltaTime, e);
				}
				List<TouchEvent> events = game.getInput().getTouchEvents();
				game.getInput().getKeyEvents();
				for(int i = 0 ; i< events.size() ; i++)
				{
					e = events.get(i);
					
					Vector2 tPoint = new Vector2(e.x, 480-e.y);
					if(e.type == TouchEvent.TOUCH_UP)
					{
						if(OverlapTester.pointInRectangle(Assets.soundButton, tPoint) && !Settings.showHelpStartScreen)
						{
							Settings.soundEnabled = !Settings.soundEnabled;
						}
						if(OverlapTester.pointInRectangle(Assets.helpButton, tPoint) && !Settings.showHelpStartScreen)
						{
							help = new Help(glGraphics, batcher, game);
							Settings.showHelpStartScreen = true;
						}
						if(OverlapTester.pointInRectangle(Assets.playButtonRectangle, tPoint)  && !Settings.showHelpStartScreen)
						{
							playButton.currentState = GeneralButton.TOUCH_UP;
							Assets.playSound(Assets.clickSound);
							game.setScreen(new LevelSelectScreen(game, 100, 800, 480) , true);
						}
					}
					
					if(e.type == TouchEvent.TOUCH_DOWN)
					{
						if(OverlapTester.pointInRectangle(Assets.playButtonRectangle, tPoint)  && !Settings.showHelpStartScreen)
						{
							playButton.currentState = GeneralButton.TOUCH_DOWN;
						}
					}
					
				}
			}
			logoLeftX += (250-logoLeftX)/10;
			logoRightX += (641-logoRightX)/10;
		}
		else
		{
			dotCounter++;
			
			if(dotCounter>10 )
			{
				dotCount++;
				if(dotCount > 3)
				{
					dotCount = 0;
				}
				dotCounter = 0;
			}
		}
	}

	@Override
	public void present(float deltaTime)
	{
		stateTime += deltaTime;
		
		GL10 gl = glGraphics.getGL();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		camera.setViewportAndMatrices();
		if(this.glGame.firtTimeAdReceived)
		{
			
			
			batcher.beginBatch(Assets.items);
			batcher.drawSprite(400, 240, 800, 480, 1, Assets.backRegion);
			batcher.drawSprite(	400, 160, Assets.playButtonRegion.width, Assets.playButtonRegion.height, 1, 
								playButton.currentState == GeneralButton.TOUCH_DOWN ? Assets.playButtonDownRegion : Assets.playButtonRegion);
			batcher.drawSprite(400, 29, Assets.controlButtonsRegion.width, Assets.controlButtonsRegion.height, 0.7f, Assets.controlButtonsRegion);
			batcher.drawSprite(363, 25, Assets.controlButtonSoundRegion.width, Assets.controlButtonSoundRegion.height, 1, Settings.soundEnabled ? Assets.controlButtonSoundRegion : Assets.controlButtonSoundOffRegion);
			batcher.drawSprite(442, 25, Assets.controlButtonHelpRegion.width, Assets.controlButtonHelpRegion.height, 1, Assets.controlButtonHelpRegion);
			batcher.endBatch();
			
			batcher.beginBatch(Assets.items2);
			batcher.drawSprite(logoLeftX, 333, Assets.logoLeftRegion.width, Assets.logoLeftRegion.height, 1, Assets.logoLeftRegion);
			batcher.drawSprite(logoRightX, 333, Assets.logoRightRegion.width, Assets.logoRightRegion.height, 1, Assets.logoRightRegion);
			batcher.endBatch();
			
			if(Settings.showHelpStartScreen)
			{
				help.present(deltaTime);
			}
		}
		else
		{
			batcher.beginBatch(Assets.items);
			batcher.drawSprite(400, 240, 800, 480, 1, Assets.backRegion);
			batcher.endBatch();
			batcher.beginBatch(Assets.items2);
			batcher.drawSprite(400, 100, 800, 64, 1, Assets.loadingSign);
			
			for(int i = 0 ; i < dotCount ; i++)
			{
				batcher.drawSprite(739 + i*14, 92, 32, 32, 1, Assets.loadingDot);
			}
			batcher.endBatch();
		}
		camera.entranceEffectZoom(Quad.OUT ,stateTime , 0.5f, 0.5f, 1f);
	}

	@Override
	public void pause()
	{
		
	}

	@Override
	public void resume()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose()
	{
		// TODO Auto-generated method stub

	}

}
